package de.haw.algorithm;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class DatenContainer<T extends Serializable> {
	private final Queue<T> produkte = new LinkedList<T>();
	private final Lock lock = new ReentrantLock();
	private final Condition nichtVoll = lock.newCondition();
	private final Condition nichtLeer = lock.newCondition();

	public DatenContainer() {
	}

	public void rein(T elem) throws InterruptedException {
		lock.lock();
		try {
			while (memoryInUse(elem))
				nichtVoll.await();

			produkte.add(elem);

			nichtLeer.signalAll();
		} finally {
			lock.unlock();
		}
	}

	private boolean memoryInUse(T elem) {
		return false;
	}

	public T raus() throws InterruptedException {
		lock.lock();
		try {
			while (produkte.size() == 0)
				nichtLeer.await();

			T elem = produkte.poll();

			nichtVoll.signalAll();
			return elem;
		} finally {
			lock.unlock();
		}
	}

	public int size() {
		lock.lock();
		try {
			return produkte.size();
		} finally {
			lock.unlock();
		}
	}
}
